package com.dc.designPattern._2_factory._3_absfactory.order;

import com.dc.designPattern._2_factory._3_absfactory.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Description: 披萨订单，演示 抽象工厂方法
 * @author: gdc
 * @date: 2021/3/7
 * @version 1.0
 */
public class OrderPizza {

	/**
	 * 抽象工厂接口
	 */
	private AbsFactory factory;

	public OrderPizza(AbsFactory factory) {
		setFactory(factory);
	}

	private void setFactory(AbsFactory factory) {
		Pizza pizza;
		String orderType;
		this.factory = factory;
		do {
			orderType = getType();

			// factory 可能是北京的工厂子类，也可能是伦敦的工厂子类
			pizza = factory.createPizza(orderType);
			if (pizza != null) {
				pizza.execute();
			} else {
				System.out.println("订购失败");
				break;
			}
		} while (true);
	}

	/**
	 * 可以获取客户希望订购的披萨种类
	 */
	private String getType() {
		try {
			BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("input pizza 种类:");
			return strin.readLine();
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}
}
